﻿using Comm520;
using RFIDSystem.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFIDSystem
{

    public partial class InStock : BaseForm
    {
        //10009
       
        DLC520Socket dlcsocket;
        public InStock()
        {
            InitializeComponent();            
            dlcsocket = this.GetSocket(SocketPort);
            dlcsocket.DataReceived += new DLC520Socket.MachineDataReceived(DataReceived);
            

        }
        private void InStock_Load(object sender, EventArgs e)
        {
            this.BindStockList();
        }         

        private void txtProductRFID_TextChanged(object sender, EventArgs e)
        {
            this.LoadProductInfo();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(txtProductId.Text=="")
            {
                MessageBox.Show("请先刷RFID，读取信息!");
                return;
            }
            stock_in stock = new stock_in();
            product_product product = DAL.product_product.Find(int.Parse(txtProductId.Text));

            if(product==null)
            {
                MessageBox.Show("基础产品库中无此RFID的产品，请先维护产品!");
                return;
            }

            stock.qty_in = int.Parse(txtInQty.Text);
            stock.local = txtLocal.Text;
            stock.create_datetime = DateTime.Now;
            stock.product_product = product;
            DAL.stock_in.Add(stock);
            DAL.SaveChanges();

            this.BindStockList();

        }

        private void LoadProductInfo()
        {
            var product = DAL.product_product.FirstOrDefault(p => p.product_rfid == txtProductRFID.Text);

            if (product == null)
            {
                MessageBox.Show("基础产品库中无此RFID的产品，请先维护产品!");
                return;
            }
            txtProductId.Text = product.product_id.ToString();
            txtProductName.Text = product.product_name;
            txtProductModel.Text = product.product_model;
            txtProductBarcode.Text = product.product_barcode;
        }

        private void BindStockList()
        {
            var query = from s in DAL.stock_in
                           select new
                           {
                               s.stock_id,
                               s.product_product.product_id,
                               s.product_product.product_catalog.product_catalog_name,
                               s.product_product.product_name,
                               s.product_product.product_model,
                               s.product_product.product_barcode,
                               s.product_product.product_rfid,
                               s.qty_in,
                               s.local,
                               s.create_datetime,
                           };
            gridList.AutoGenerateColumns = false;
            gridList.DataSource = query.ToList();
        }

       
        private void gridList_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow row = gridList.CurrentRow;           
            

            txtProductName.Text = row.Cells["ProductName"].Value.ToString();
            txtProductModel.Text = row.Cells["ProductModel"].Value.ToString();
            txtProductBarcode.Text= row.Cells["ProductBarcode"].Value.ToString();
            txtProductRFID.Text = row.Cells["ProductRFID"].Value.ToString();
            txtInQty.Text = row.Cells["qryin"].Value.ToString();
            txtLocal.Text = row.Cells["local"].Value.ToString();
            
        }

       
        private void btnModify_Click(object sender, EventArgs e)
        {
            int currentId = (int)gridList.CurrentRow.Cells["StockId"].Value;
            stock_in stock = new stock_in() { stock_id = currentId };
            stock.qty_in = int.Parse(txtInQty.Text);
            stock.local = txtLocal.Text;
            stock.create_datetime = DateTime.Now;
            DAL.Entry(stock).State = EntityState.Modified;
            DAL.SaveChanges();
            BindStockList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int currentId = (int)gridList.CurrentRow.Cells["StockId"].Value;
            stock_in stock = new stock_in() { stock_id = currentId };
            DAL.Entry(stock).State = EntityState.Deleted;
            DAL.SaveChanges();
            BindStockList();
        }

        bool DataReceived(string data)
        {
            try
            {

                //datetime=20160507163510,ip=192.168.1.10,result=15,event=1,cardx4=54585382,mdno=487,redio=0,photono=32,
                string cardid = "", ip = "", datetime = "", occur = "", result = "", before = "", after = "", mdno = "", cdno = "", dealtype = "", macno = "", serno = "", status = "";
                string[] items = data.Split(',');
                for (int index = 0; index < items.Length; index++)
                {
                    if (items[index].IndexOf("cardid", 0) >= 0 || items[index].IndexOf("cardx4", 0) >= 0)
                    {
                        cardid = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("ip", 0) >= 0)
                    {
                        ip = items[index].Substring(3);
                    }
                    if (items[index].IndexOf("datetime", 0) >= 0)
                    {
                        datetime = items[index].Substring(9);
                    }
                    if (items[index].IndexOf("occur", 0) >= 0)
                    {
                        occur = items[index].Substring(6);
                    }
                    if (items[index].IndexOf("result", 0) >= 0)
                    {
                        result = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("before", 0) >= 0)
                    {
                        before = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("after", 0) >= 0)
                    {
                        after = items[index].Substring(6);
                    }
                    if (items[index].IndexOf("mdno", 0) >= 0)
                    {
                        mdno = items[index].Substring(5);
                    }
                    if (items[index].IndexOf("cdno", 0) >= 0)
                    {
                        cdno = items[index].Substring(5);
                    }
                    if (items[index].IndexOf("dealtype", 0) >= 0)
                    {
                        dealtype = items[index].Substring(9);
                    }
                    if (items[index].IndexOf("status", 0) >= 0)
                    {
                        status = items[index].Substring(7);
                    }
                    if (items[index].IndexOf("macno", 0) >= 0)
                    {
                        macno = items[index].Substring(6);
                    }
                    if (items[index].IndexOf("serno", 0) >= 0)
                    {
                        serno = items[index].Substring(6);
                    }

                }

                this.Invoke(new Action<TextBox, string>((t, s) => t.Text = s), new object[] { txtProductRFID, cardid });
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            var printForm = new InStockPrint();
            printForm.BeginDate = DateTime.Now.AddDays(-30);
            printForm.EndDate = DateTime.Now.AddDays(0);
            printForm.Show();
        }
    }
}
